import * as monaco from "monaco-editor";
monaco.languages.register({ id: "mylang" });
monaco.editor.defineTheme("myTheme", {
  //基础
  base: "vs-dark",
  //继承
  inherit: true,
  //规则
  rules: [
	{ token: "my-built-in-function", foreground: "ff0000", fontStyle: 'bold' },
	{ token: "my-keyword", foreground: "FFA500" },
	{ token: "my-function", foreground: "008800" },
  ],
});
monaco.languages.setMonarchTokensProvider("mylang", {
  tokenizer: {
	root: [
	  [/[{}()\[\]]/, '@brackets'],

	  [/\b\d*\.\d+([eE][\-+]?\d+)?\b/, "number.float"],
	  [/\b0[xX][0-9a-fA-F]+\b/, "number.hex"],
	  [/\b\d+\b/, "number"],

	  [/\b(if|else|return)\b/, "my-keyword"],

	  [/\b(and|or|not)\b/, "my-built-in-function"],

	  [/([a-zA-Z0-9_]+)\s*(\(.*\))/, "@rematch", "@matchfunc"],

	  [/'([^'\\]|\\.)*$/, "string.invalid"],
	  [/'/, { token: "string.quote", bracket: "@open", next: "@string" }],
	  { include: "@whitespace" },
	],
	matchfunc: [
	  [/([a-zA-Z0-9_]+)/, { token: "my-function", next: "@popall"}],
	],
	whitespace: [
	  [/[ \t\r\n]+/, "white"]
	],
  },
  //语言大小写不敏感吗
  ignoreCase: true,
});
monaco.languages.setLanguageConfiguration("mylang", {
  brackets: [
    ["{", "}"],
    ["[", "]"],
    ["(", ")"],
  ],
  autoClosingPairs: [
    { open: "{", close: "}" },
    { open: "[", close: "]" },
    { open: "(", close: ")" },
    { open: "'", close: "'", notIn: ["string", "comment"] }
  ],
});
monaco.editor.create(document.getElementById("container"), {
  language: "mylang",
  theme: "myTheme",
  wordWrapColumn: 120,
  wordWrapMinified: true
});